package pzl.gif.loader;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
    }

    static ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = findViewById(R.id.iv);

        trans();
        stringFromJNI();

        String gifpath = new File(getFilesDir().getAbsolutePath() + File.separator + "demo.gif").getPath();
        Loader.init(gifpath);

        Log.i("jinxJava", "宽 = " + Loader.gifWidth());
        Log.i("jinxJava", "高 = " + Loader.gifHeight());
        Log.i("jinxJava", "帧数 = " + Loader.gifFrameCount());

    }

    H h = new H();
    static Bitmap frameBitmap;
    static int cur;
    static int total;

    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_loader:
                frameBitmap = Bitmap.createBitmap(Loader.gifWidth(), Loader.gifHeight(), Bitmap.Config.ARGB_8888);
                total = Loader.gifFrameCount();
                cur = 0;
                new Thread() {
                    @Override
                    public void run() {
                        while (cur < total) {
                            long delay = Loader.gifRender(frameBitmap, cur);
                            Message msg = Message.obtain();
                            msg.arg1 = cur;
                            h.sendMessage(msg);
                            try {
                                Thread.sleep(delay);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
                break;
        }
    }

    static class H extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            iv.setImageBitmap(frameBitmap);
            if (cur < total) cur++;
        }
    }

    public native String stringFromJNI();

    private void trans() {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            is = getAssets().open("demo.gif");
            int len = is.available();
            File file = new File(getFilesDir().getAbsolutePath() + File.separator + "demo.gif");
            fos = new FileOutputStream(file.getPath());
            byte buf[] = new byte[64];
            int temp = 0;
            while ((temp = is.read(buf)) != -1) {
                fos.write(buf, 0, temp);
            }
            fos.flush();
            fos.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                fos.close();
                is.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

}
